package org.erp.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.erp.common.page.PageQueryModel;
import org.erp.product.domain.dto.DickerDto;
import org.erp.product.domain.dto.ProductInfoQueryDto;
import org.erp.product.domain.dto.TransferLogAddDto;
import org.erp.product.domain.entity.ProductInfo;
import org.erp.product.domain.vo.ProductInfoVo;
import org.erp.product.infrastructure.config.InspectionStateFlow;
import org.erp.product.infrastructure.convert.ProductInfoConvert;
import org.erp.product.infrastructure.enums.InspectionEventEnum;
import org.erp.product.infrastructure.enums.InspectionStatusEnum;
import org.erp.product.infrastructure.exception.ProductException;
import org.erp.product.infrastructure.mapper.ProductInfoMapper;
import org.erp.product.service.ProductInfoService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 产品信息服务实现层
 *
 * @author libin
 * @version 1.0
 * @description: 产品信息服务实现层
 * @date 2025/11/13 16:15
 **/
@Log4j2
@Service
@AllArgsConstructor
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, ProductInfo> implements ProductInfoService {

    private final ProductInfoConvert productInfoConvert;

    @Override
    public PageQueryModel<ProductInfoVo> pageList(ProductInfoQueryDto productInfoQueryDto) {
        Page<ProductInfo> page = this.page(productInfoQueryDto.buildPage(), new QueryWrapper<ProductInfo>().lambda()
                .eq(null != productInfoQueryDto.getCategoryId(), ProductInfo::getCategoryId, productInfoQueryDto.getCategoryId())
                .eq(null != productInfoQueryDto.getBrandId(), ProductInfo::getBrandId, productInfoQueryDto.getBrandId())
                .eq(null != productInfoQueryDto.getLevelId(), ProductInfo::getLevelId, productInfoQueryDto.getLevelId())
                .like(StringUtils.isNotBlank(productInfoQueryDto.getProductImei()), ProductInfo::getProductImei, productInfoQueryDto.getProductImei())
                .like(StringUtils.isNotBlank(productInfoQueryDto.getProductCode()), ProductInfo::getProductCode, productInfoQueryDto.getProductCode())
                .eq(null != productInfoQueryDto.getTenantId(), ProductInfo::getTenantId, productInfoQueryDto.getTenantId())
                .eq(null != productInfoQueryDto.getInspectionStatus(), ProductInfo::getInspectionStatus, productInfoQueryDto.getInspectionStatus())
                .ge(null != productInfoQueryDto.getInTransitDay(), ProductInfo::getInTransitDay, productInfoQueryDto.getInTransitDay())
                .ge(null != productInfoQueryDto.getInStockDay(), ProductInfo::getInStockDay, productInfoQueryDto.getInStockDay())
                .ge(null != productInfoQueryDto.getStartTime(), ProductInfo::getCreateTime, productInfoQueryDto.getStartTime())
                .le(null != productInfoQueryDto.getEndTime(), ProductInfo::getCreateTime, productInfoQueryDto.getEndTime()));
        return PageQueryModel.of(page.getPages(), page.getRecords().stream().map(productInfoConvert::convertVo).toList());
    }

    @Override
    public List<ProductInfo> searchAndVerify(List<DickerDto> dickerDtoList, InspectionEventEnum inspectionEventEnum) {
        Set<Long> stockIds = dickerDtoList.stream().map(DickerDto::getStockId).collect(Collectors.toSet());
        List<ProductInfo> list = this.list(new QueryWrapper<ProductInfo>().lambda()
                .in(ProductInfo::getStockId, stockIds));
        for (ProductInfo productInfo : list) {
            try {
                InspectionStateFlow.next(InspectionStatusEnum.getByCode(productInfo.getInspectionStatus()), inspectionEventEnum);
            } catch (Exception e) {
                throw new ProductException("产品操作事件id为: " + productInfo.getId() + " 状态不允许执行该操作");
            }
        }
        return list;
    }

    @Override
    public TransferLogAddDto updateBatchStatus(List<ProductInfo> productInfoList, InspectionEventEnum inspectionEventEnum) {
        Integer oldStatus = productInfoList.get(0).getInspectionStatus();
        productInfoList.forEach(w -> {
            InspectionStatusEnum nextStatus = InspectionStateFlow.next(InspectionStatusEnum.getByCode(w.getInspectionStatus()), inspectionEventEnum);
            w.setInspectionStatus(nextStatus.getCode());
        });
        this.baseMapper.updateBatchById(productInfoList);
        TransferLogAddDto transferLogAddDto = new TransferLogAddDto();
        transferLogAddDto.setOldStatus(oldStatus);
        transferLogAddDto.setProductInfoList(productInfoList);
        return transferLogAddDto;
    }
}
